package kuoge.practice.kafka.stream.member;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;

import java.util.Properties;

/**
 * @Description
 */
public class KafkaProducerFactory {
    private static final Properties props = new Properties();
    private static final Producer<String, String> kafkaProducer;

    static {
        props.put("bootstrap.servers", "localhost:9092");
        props.put("linger.ms", 1);
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
        kafkaProducer = new KafkaProducer<>(props);

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                kafkaProducer.close();
                System.out.println("stop kafka producer success");
            }
        });
    }

    public static Producer<String, String> getInstance() {
        return kafkaProducer;
    }
}
